package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsFinance;
import net.anzhuo.entity.db.Finance;
import net.anzhuo.entity.db.FinanceResult;
import net.anzhuo.entity.db.Paging;
import net.anzhuo.service.GsFinanceService;
/**
 * 
 * @author jiyk
 *   融资
 */
@RestController
@Slf4j
public class FinancingController {

	@Autowired
	private GsFinanceService gsFinanceService;

	
	@RequestMapping("/Finance/FinanceInfo")
	public FinanceResult FinanceInfo(String type,String searchKey,String pageIndex, String pageSize) {
		log.info("===>getFinanceInfo_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
	    FinanceResult rr = null;
		try {
			rr = (FinanceResult)RedisUtil.get("FinanceResult:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" FinanceResult 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsFinance gsFinanceQuery = new GsFinance();
	//	GsBase base = gsBaseService.getByName(searchKey);
		gsFinanceQuery.setCoMd5(searchKey);
		gsFinanceQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsFinance> gsFinanceList = gsFinanceService.getListWithPage(gsFinanceQuery);
		System.out.println("keyno="+searchKey+",gsFinanceList="+gsFinanceList.getList().size());
		log.info("keyno="+searchKey+",openCourtList="+gsFinanceList.getList().size());
		FinanceResult responseResult = new FinanceResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(gsFinanceList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<Finance> resultList = new ArrayList<Finance>();
		Finance result = null;
		if (gsFinanceList.getList()!=null && gsFinanceList.getList().size()>0) {
			for (GsFinance g : gsFinanceList.getList()) {
				result = new Finance();
		        result.setDateTime(g.getDatetime());
		        result.setRotation(g.getRotation());
		        result.setMoney(g.getMoney());
		        result.setInvestor(g.getInvestor());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("FinanceResult:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" FinanceResult 执行时间："+excTime+"s");
		return responseResult;
	}
	
	
	
	
	
}
